package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.ShoppingCartBean;
import com.project.mapper.IShoppingCartMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ShoppingCartServiceImp implements IShoppingCartService {
    @Autowired
    private IShoppingCartMapper shopMapper;
    @Override
    public void buy(Integer productId, Integer userId) {
        QueryWrapper<ShoppingCartBean> qw = new QueryWrapper<>();
        qw.eq("fk_productId",productId);
        qw.eq("fk_userId",userId);
        ShoppingCartBean shoppingCartBean =shopMapper.selectOne(qw);
        if(shoppingCartBean==null){//不存在
            ShoppingCartBean newShopBean = new ShoppingCartBean(userId,productId,1);//默认买一件
            shopMapper.insert(newShopBean);
        }else {//存在
            shoppingCartBean.setBuyNum(shoppingCartBean.getBuyNum()+1);
            shopMapper.updateById(shoppingCartBean);
        }
    }

    @Override
    public void remove(Integer shopId) {
        shopMapper.deleteById(shopId);
    }

    @Override
    public void updateNum(Integer shopId, Integer buyNum) {
        ShoppingCartBean shoppingCartBean =shopMapper.selectById(shopId);
        shoppingCartBean.setBuyNum(buyNum);
        shopMapper.updateById(shoppingCartBean);
    }

    @Override
    public List<ShoppingCartBean> findByUserId(Integer userId) {
        return shopMapper.findByUserId(userId);
    }
}
